# -*- coding:utf-8 -*-
import sys
import os
from string import Template
from python import utils
from python import hive_conn
reload(sys)
sys.setdefaultencoding('utf-8')


if __name__ == "__main__" :
	sql_file_name=sys.argv[1]
	print "the sql file is "+sql_file_name
	global_properties=utils.get_global_properties()
	print global_properties
	username=global_properties['hive_username']
	password=None
	if global_properties.has_key('hive_password'):
		password=global_properties['hive_password']
	host=global_properties["hive_thrift_server_host"]
	port=int(global_properties["hive_thrift_server_port"])
	database=global_properties["hive_default_database"]
	partition=utils.get_partition_by_flow_type(global_properties['flow_type'],global_properties)
	global_properties['partition']=partition
	print "the partition",partition
	sql=""
	with open(sql_file_name,'r') as f:
		for line in f:
			sql=sql+line
	print "========================="
	print sql
	print "========================="
	tpl =Template(sql)
	result=tpl.substitute(global_properties)
	print "========================="
	print result
	print "========================="
	hive_conn.query(host,port,database,username,result,password)